--
-- Author: yawei.zhu
-- Date: 2018-09-05 18:01:43
--
local Vector = require("app.utils.Vector")

local Goods = class("Goods")

function Goods:ctor(params)
    self.id = params.id--商品ID
    self.itemType = params.itemType --  enum LootType{item=1; weapon=2;mask=3;horse=4;fashion=5;merc=11;equip=12;}
    self.itemId = params.itemId--道具ID

    self.conds = Vector.new()
    if params.conds then
        for i,v in ipairs(params.conds) do
            local condition = {}
            condition.condType = v.condType --购买要求-类型 --None没要求 Fame声望要求 Level等级要求 Vip vip等级要求 Achievement成就要求
            condition.condID = v.condID --购买要求-ID
            condition.condValue = v.condValue -- 购买要求-数值/等级

            self.conds:add(condition)
        end
    end

    --self.currency = params.currency--货币类型 enum ECurrency {gold = 1;// 金币 coin = 2;// 代币}
    --self.price = params.price--售价
    self.numType = params.numType--数量限制类型   enum NumType {NoLimit = 0;// 没有限制 DayLimit = 1;// 每日限制 OneLifeLimit = 2;// 一生限制}
    self.limitNum = params.limitNum--限制数量
    self.leftNum = params.leftNum--剩余数量(可购次数)
    self.cost = Vector.new() -- 一个商品需要多个道具购买
    if params.cost then
        for i, v in ipairs(params.cost) do
            local cost = {}
            cost.type = v.type
            cost.id = v.id
            cost.num = v.num
            self.cost:add(cost)
        end
    end 
    self.price = self:getPriceFromCost() -- 用单个道具购买获取购买价格

    self.show = false
    local goodConfig = GD:queryGoodById(self.id)
    if goodConfig and goodConfig.classshow then
        if tonumber(goodConfig.classshow) ~= 0 then
            local classIds = string.split(goodConfig.classshow,"|")
            for i,id in ipairs(classIds) do
                if UD:getPlayer().classID == tonumber(id) then
                    self.show = true
                    break
                end
            end
        else
            self.show = true
        end
    else
        self.show = true
    end
    self.selNum = 1
    self.selName = ""
    if goodConfig and goodConfig.item then
        self.selName = L( goodConfig.name )
        local array = string.split(goodConfig.item,"|")
        if #array >= 3 then
            self.selNum = tonumber(array[3])
        end
    end

    --出售道具
    local iType = protobuf.enum_id("pb_loot.Loot.LootType", self.itemType)
    local itemF = iType .. "|" .. self.itemId .. "|" .. self.selNum
    local itemData = UD:parseRewardToBagItem(itemF)
    if itemData then
        self.qualityBg = itemData:getIconBg()
        self.icon = itemData:getIcon()
    end
    self.itemData = itemData

    --显示专用
    if goodConfig and goodConfig.fakeShow then
        local array = string.split(goodConfig.fakeShow,"|")
        if #array >= 3 then
            self.selNum = tonumber(array[3])

            itemData = UD:parseRewardToBagItem(goodConfig.fakeShow)
            if itemData then
                self.qualityBg = itemData:getIconBg()
                self.icon = itemData:getIcon()
            end
            self.fakeItemData = itemData
        end
    end


    -- print("Goods.ctor ------- self.currency = ",self.currency)
    -- print("Goods.ctor ------- self.price = ",self.price)
    -- print("Goods.ctor ------- self.numType = ",self.numType)
    -- print("Goods.ctor ------- self.limitNum = ",self.limitNum)
    -- print("Goods.ctor ------- self.leftNum = ",self.leftNum)
end

-- 返回消耗的资源类型
-- enum LootType{
--     none        = 0;//无类型
--     //道具类型
--     equip       = 2;  //装备
--     item        = 1;  //物品
--     jewel       = 8;  //宝石
--     //货币类型
--     bindCoin    = 87; //绑钻
--     magicDust   = 92; //魔尘
--     coin        = 98; //钻石
--     gold        = 99; //金币
--     //资源类型
--     skillPoint  = 85; //技能点
--     talentPoint = 86; //天赋点
--     charm       = 89; //好运符
--     power       = 96; //体力值
--     exp         = 97; //经验值
--     fame        = 100;//声望值
--     lifeSkill   = 88; //生活技能经验
--     //玩法类型
--     mount       = 4;  //坐骑
--     rune        = 7;  //符文
--     partner     = 11; //随从
--     tool        = 5;  //工具
--     formula     = 9;  //formula
--     mapBuff     = 21; //地图BUFF
--     title       = 83; //玩家称号
--     eventPoint  = 91; //世界事件活动点
-- }
function Goods:getCostTypeId()
    local cost = self.cost[1]
    if cost then
        if "bindCoin" == cost.type then
            return 87
        elseif "magicDust" == cost.type then
            return 92
        elseif "coin" == cost.type then
            return 98
        elseif "gold" == cost.type then
            return 99
        elseif "skillPoint" == cost.type then
            return 85
        elseif "talentPoint" == cost.type then
            return 86
        elseif "power" == cost.type then
            return 96
        elseif "roseGold" == cost.type then
            return 110
        elseif "magicCrystal" == cost.type then
            return 115
        end
    end
end

function Goods:getPriceFromCost()
    if not self.cost then return end
    if #self.cost == 1 then
        return self.cost[1].num
    end
end

function Goods:getDesc()
    local goodConfig = GD:queryGoodById(self.id)
    if goodConfig then
        return L(goodConfig.desc)
    end
end

function Goods:read(params)
    self.id = params.id--商品ID
    self.itemType = params.itemType --  enum LootType{item=1; weapon=2;mask=3;horse=4;fashion=5;merc=11;equip=12;}
    self.itemId = params.itemId--道具ID

    self.conds = params.conds

    self.currency = params.currency--货币类型 enum ECurrency {gold = 1;// 金币 coin = 2;// 代币}
    self.price = params.price--售价
    self.numType = params.numType--数量限制类型   enum NumType {NoLimit = 0;// 没有限制 DayLimit = 1;// 每日限制 OneLifeLimit = 2;// 一生限制}
    self.limitNum = params.limitNum--限制数量
    self.leftNum = params.leftNum--剩余数量(可购次数)
end

return Goods
